package huoye.shops.huoye.operation.activity;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codeideology.android.utils.IntentUtils;
import com.codeideology.android.utils.StringUtils;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.ButtomClickInterval;
import huoye.shops.base.UserManager;
import huoye.shops.huoye.operation.adapter.OrderDetailsShopMessageAdapter;
import huoye.shops.huoye.operation.adapter.OrderDetailsStateAdapter;
import huoye.shops.huoye.operation.model.OrderInfo;
import huoye.shops.huoye.operation.model.PlatformInfo;
import huoye.shops.huoye.operation.model.SellOrder;
import huoye.shops.huoye.operation.presenter.OrderDetailsPresenter;
import huoye.shops.huoye.operation.view.IOrderDetailsView;
import huoye.shops.huoye.operation.widget.OrderDetailsPhomeDialog;


public class  OrderDetailsActivity extends BaseActivity implements IOrderDetailsView {


    @BindView(R.id.order_details_tv_contact)
    TextView tvContact;
    @BindView(R.id.order_details_tv_phone)
    TextView tvPhone;
    private OrderDetailsPresenter mOrderDetailsPresenter;


    @OnClick(R.id.order_details_ll_phone)
    void onCallPhoneClick() {
      //  showPhone();
    }

    @BindView(R.id.order_details_tv_address)
    TextView tvAddress;

    @BindView(R.id.order_details_tv_tatal)
    TextView tvTatal;

    @BindView(R.id.order_details_tv_manage)
    TextView tvManage;



    @BindView(R.id.order_details_rv_order_state)
    RecyclerView rvOrderState;
    @BindView(R.id.order_details_rv_shop_manage)
    RecyclerView rvShopManage;

    //取消订单
    @BindView(R.id.order_details_btn_order_cancel)
    Button btnCancel;
    @BindView(R.id.order_details_btn_send_my)
    Button btnSendMy;
    @BindView(R.id.order_details_btn_send_terrace)
    Button btnSendTerrace;

    @BindView(R.id.order_details_ll_send)
     LinearLayout llSend;
    /*@BindView(R.id.order_details_btn_fulfill)
    Button btnFulfill;*/


    //取消订单
    @OnClick(R.id.order_details_btn_order_cancel)
    void onOrderCancelClick() {
         SendType = "";
         mOrderDetailsPresenter.setOrderState(mOrderInfo.getOrderId(),"cancel",SendType);
    }
    //自己配送
    @OnClick(R.id.order_details_btn_send_my)
    void onSendMineClick() {
        SendType = "1";
        if(ButtomClickInterval.isFastClick()){
            mOrderDetailsPresenter.setOrderState(mOrderInfo.getOrderId(),"send",SendType);
        }

    }
    //平台配送    确认是否已送达
    @OnClick(R.id.order_details_btn_send_terrace)
    void onSendTerraceClick() {
        if(ButtomClickInterval.isFastClick()){
            if(isSend){
                mOrderDetailsPresenter.setOrderState(mOrderInfo.getOrderId(),"sendok","");
            }else {
                mOrderDetailsPresenter.getPlatformInfo();
            }
        }
    }


    private String SendType = "";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_order_details;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("订单详情");

        SellOrder mSellOrder = getIntentData();


        mOrderDetailsPresenter = new OrderDetailsPresenter();
        mOrderDetailsPresenter.addTaskListener(this);
        mOrderDetailsPresenter.onStart();
        mOrderDetailsPresenter.getOrderInfo(mSellOrder.getOrderId());
    }




    private String ShipPhone;
    private OrderInfo mOrderInfo;


    @Override
    public void orderDetails(OrderInfo orderInfo) {
      this.mOrderInfo = orderInfo;

        tvAddress.setText(orderInfo.getAddress());
        tvPhone.setText(orderInfo.getShipTel());
        ShipPhone = orderInfo.getShipTel();
        tvContact.setText(orderInfo.getShipName());

        OrderDetailsStateAdapter orderDetailsStateAdapter = new OrderDetailsStateAdapter(this);
        if (orderInfo.getOrderRecords() != null) {
            orderDetailsStateAdapter.setData(orderInfo.getOrderRecords());
        }
        rvOrderState.setLayoutManager(new LinearLayoutManager(this));
        rvOrderState.setAdapter(orderDetailsStateAdapter);


        OrderDetailsShopMessageAdapter orderDetailsShopMessageAdapter = new OrderDetailsShopMessageAdapter(this);
        if (orderInfo.getOrderProducts() != null) {
            orderDetailsShopMessageAdapter.setData(orderInfo.getOrderProducts());
        }
        rvShopManage.setLayoutManager(new LinearLayoutManager(this));
        rvShopManage.setAdapter(orderDetailsShopMessageAdapter);


        float payMoney = Float.valueOf(orderInfo.getPayMoney());
        float reduceMoney = Float.valueOf(orderInfo.getReduceMoney());
        tvTatal.setText((payMoney+reduceMoney)+"");
        tvManage.setText(orderInfo.getRemark());



        switch (orderInfo.getiState()){
            case 1:
                //待付款
                llSend.setVisibility(View.INVISIBLE);
                break;
            case 2:
                //待发货
                llSend.setVisibility(View.VISIBLE);
                break;
            case 3:
                //已发货
                btnSendMy.setVisibility(View.GONE);
                btnSendTerrace.setText("已送达");
                isSend = true;
                break;
            case 4:
                //已送达
                llSend.setVisibility(View.INVISIBLE);
                break;
            case 5:
                //已完成
          /*      btnSendMy.setVisibility(View.GONE);
                btnSendTerrace.setText("已完成");
                btnSendTerrace.setEnabled(false);
                btnSendTerrace.setSelected(false);*/
                llSend.setVisibility(View.INVISIBLE);
                break;
            case 6: //退货中
                llSend.setVisibility(View.INVISIBLE);
                break;
            case 7:
                //已取消
                btnSendMy.setVisibility(View.GONE);
                btnSendTerrace.setText("已取消");
                btnSendTerrace.setEnabled(false);
                btnSendTerrace.setSelected(false);
                break;
        }
    }

    //是否送达
    private boolean isSend = false;

    @Override
    public void getPlatformInfoData(PlatformInfo info) {
        if(StringUtils.isNotEmpty(info.getDeliveryTel())){
            showPhone(info.getDeliveryTel());
        }else {
            makeToast("平台暂无配送");
        }

    }



    private void showPhone(String tel) {
        OrderDetailsPhomeDialog orderDetailsPhomeDialog = new OrderDetailsPhomeDialog(this);
        orderDetailsPhomeDialog.setOnPhoneListener(new OrderDetailsPhomeDialog.OnPhoneListener() {
            @Override
            public void onPhoneClick() {
                IntentUtils.callPhone(OrderDetailsActivity.this,tel);
            }

            @Override
            public void onSendClick() {
                SendType = "2";
                mOrderDetailsPresenter.setOrderState(mOrderInfo.getOrderId(),"send",SendType);
            }
        });
        orderDetailsPhomeDialog.show();
        orderDetailsPhomeDialog.setPhone(tel);
        orderDetailsPhomeDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失.
    }
}
